<?php
/**
 * 地方车展-项目模型
 * @author jianhui
 *
 */
class carProjectModel extends MY_Model{
    function carProjectModel(){
        parent::MY_Model();
        $this->table = 'CarProject';
    }
    /**
     * 更新父类金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('carProcessModel');
        $this->CI->carProcessModel->updateMoney($parentId);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['procId']) && $data['procId'] ? (int)$data['procId'] : 0;
    }
    /**
     * 数据包装
     * @param array $list
     */
    function packing($list = array()){
        //专营店缓存
        $this->CI->load->model('sellpointModel');
        $allStoreList = $this->CI->sellpointModel->getCacheData('allStoreList');
        //车展等级配置
        $level = $this->config->item('carshowLevel');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            //专营店名称
            $list[$k]['storeName'] = isset($allStoreList[$v['storeId']]) ? $allStoreList[$v['storeId']]['storeName'] : '';
            //车展等级
            $list[$k]['levelStr'] = isset($level[$v['level']]) ? $level[$v['level']] : '';
            //预算审核状态
            $list[$k]['budgetAuditStr'] = isset($roleList[$v['budgetAuditRole']]) ? $roleList[$v['budgetAuditRole']].'已审核' : '';
            if ($list[$k]['budgetAuditStr']){
                $list[$k]['budgetAuditStr'] .= isset($auditRs[$v['budgetAuditRs']]) ? ','.$auditRs[$v['budgetAuditRs']] : '';
            }else{
                $list[$k]['budgetAuditStr'] = isset($auditRs[$v['budgetAuditRs']]) ? $auditRs[$v['budgetAuditRs']] : '未审核';
            }
            //结算审核状态
            $list[$k]['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($list[$k]['settleAuditStr']){
                $list[$k]['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $list[$k]['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '未审核';
            }
        }
        return $list;
    }
    /**
     * 获取子类金额明细
     * @param int $projectId 车展序号
     * @return multitype:multitype:number  unknown multitype:unknown
     */
    function getDetailMoney($projectId){
        //车展广宣费用
        $this->CI->load->model('carGxModel');
        $gxMoney = $this->CI->carGxModel->getMoney($projectId);
        $data['gx'] = $gxMoney;
        //车展场租费用
        $this->CI->load->model('carRentModel');
        $rentMoney = $this->CI->carRentModel->getMoney($projectId);
        $data['rent'] = $rentMoney;
        //车展配套费用
        $this->CI->load->model('carMatchingModel');
        $matchingMoney = $this->CI->carMatchingModel->getMoney($projectId);
        $data['matching'] = $matchingMoney;
        //合计
        $data['total'] = array(
            'budgetApply' => $gxMoney['budgetApply'] + $rentMoney['budgetApply'] + $matchingMoney['budgetApply'],
            'budgetAudit' => $gxMoney['budgetAudit'] + $rentMoney['budgetAudit'] + $matchingMoney['budgetAudit'],
            'settleApply' => $gxMoney['settleApply'] + $rentMoney['settleApply'] + $matchingMoney['settleApply'],
            'settleAudit' => $gxMoney['settleAudit'] + $rentMoney['settleAudit'] + $matchingMoney['settleAudit'],
        );
        return $data;
    }
    /**
     * 更新项目金额
     * @param int $projectId
     */
    function updateMoney($projectId){
        $childMoney = $this->getDetailMoney($projectId);
        $findData = $this->getData($projectId);
        $data = array(
            'budgetApplyGx' => $childMoney['gx']['budgetApply'],
            'budgetApplyRent' => $childMoney['rent']['budgetApply'],
            'budgetApplyMatching' => $childMoney['matching']['budgetApply'],
            'budgetApply' => $childMoney['total']['budgetApply'],
            'budgetAudit' => $childMoney['total']['budgetAudit'],
            'subsidyGx' => $childMoney['gx']['subsidyMoney'],
            'settleApplyGx' => $childMoney['gx']['settleApply'],
            'settleApplyRent' => $childMoney['rent']['settleApply'],
            'subsidyMatching' => $childMoney['matching']['subsidyMatching'],
            'settleApplyMatching' => $childMoney['matching']['settleApply'],
            'settleStoreTotal' => $childMoney['rent']['settleStoreTotal'],//参展专营店实际出资总额
            'settleQiTotal' => $childMoney['rent']['settleQiTotal'],//启辰店实际出资总额
            'settleRmpSupport' => $childMoney['rent']['settleRmpSupport'],//RMP支持场租费用
            'settleApply' => $childMoney['total']['settleApply'],
            'settleAudit' => $childMoney['total']['settleAudit'],
            'isSync'=>0,
        );
        $this->edit($projectId, $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param int $procId
     * @return array
     */
    function getMoney($procId){
        $data = array(
            'siteNum' => 0,//站点数
            'budgetApplyGx' => 0,//广宣预算申请费用
            'budgetApplyRent' => 0,//场租预算申请费用
            'budgetApplyMatching' => 0,//车展配套预算费用
            'budgetApply' => 0,//预算申请
            'budgetAudit' => 0,//预算审核
            'subsidyGx' => 0,//广宣活动补贴
            'settleApplyGx' => 0,//广宣结算申请费用
            'settleApplyRent' => 0,//场租结算申请费用
            'settleApplyMatching' => 0,//车展配套结算申请费用
            'subsidyMatching' => 0,//车展配套额度
            'settleStoreTotal' => 0,//参展专营店实际出资总额
            'settleQiTotal' => 0,//启辰店实际出资总额
            'settleRmpSupport' => 0,//RMP支持场租费用
            'settleApply' => 0,//结算申请
            'settleAudit' => 0,//结算审核
        );
        $findList = $this->getList(array('procId' => $procId));
        foreach ($findList as $v){
            if ($v['isBubao'] == 0){
                $data['siteNum'] ++;
                $data['budgetApplyGx'] += $v['budgetApplyGx'];
                $data['budgetApplyRent'] += $v['budgetApplyRent'];
                $data['budgetApplyMatching'] += $v['budgetApplyMatching'];
                $data['budgetApply'] += $v['budgetApply'];
                $data['budgetAudit'] += $v['budgetAudit'];
                $data['settleApplyGx'] += $v['settleApplyGx'];
                $data['settleApplyRent'] += $v['settleApplyRent'];
                $data['settleApplyMatching'] += $v['settleApplyMatching'];
                $data['settleStoreTotal'] += $v['settleStoreTotal'];
                $data['settleQiTotal'] += $v['settleQiTotal'];
                $data['settleRmpSupport'] += $v['settleRmpSupport'];
                $data['settleApply'] += $v['settleApply'];
                $data['settleAudit'] += $v['settleAudit'];
            }else{
                
            }
        }
        return $data;
    }
    /**
     * 获取结算各状态数量(结算页需要用到，主要用作统计)
     * @param int $singleId
     * @param number $isBubao
     * @return multitype:number
     */
    function getSettleStatusCount($procId, $isBubao=0){
        $count = array(
            -1  => 0,//未提交
            0   => 0,//未审核
            1   => 0,//已通过
            2   => 0,//不通过
            3   => 0,//返回修改
            4   => 0,//部分通过
        );
        $findList = $this->getList(array('procId'=>$procId, 'isBubao'=>$isBubao, 'budgetAuditRs' => array('in',array(1,4)) ));
        foreach ($findList as $v){
            if (isset($count[$v['settleAuditRs']])){
                $count[$v['settleAuditRs']] ++;
            }
        }
        return $count;
    }
    /**
     * 项目预算一键审核
     * @param int $id
     * @param number $audit
     * @param array $data
     * @return boolean
     */
    function budgetOneKeyAudit($projectId, $audit = 1, $user = array()){
        $this->CI->load->model('carGxModel');
        $this->CI->load->model('carMatchingModel');
        $this->CI->load->model('carRentModel');
        $this->CI->carGxModel->budgetAuditByProjectId($projectId, $audit,$user);
        $this->CI->carMatchingModel->budgetAuditByProjectId($projectId, $audit, $user);
        $this->CI->carRentModel->budgetAuditByProjectId($projectId, $audit, $user);
        $this->edit($projectId, array('budgetAuditRole'=>$user['userRole']));
        $this->budgetUpdateStatus($projectId);
        //更新金额
        $this->updateMoney($projectId);
        return true;
    }
    /**
     * 更新预算审核状态
     * @param number $id 项目ID
     * @return object
     */
    function budgetUpdateStatus($projectId){
        return $this->edit($projectId, array('budgetAuditRs'=>$this->getBudgetStatus($projectId)));
    }
    /**
     * 获取项目预算审核状态值（预算审核统计，获取预算审核结果）
     * @param int $projectId
     * @return number
     */
    function getBudgetStatus($projectId){
        $this->CI->load->model('carGxModel');
        $this->CI->load->model('carMatchingModel');
        $this->CI->load->model('carRentModel');
        $gxStatusCount = $this->CI->carGxModel->getBudgetStatusCount($projectId);
        $matchingStatusCount = $this->CI->carMatchingModel->getBudgetStatusCount($projectId);
        $rentStatusCount = $this->CI->carRentModel->getBudgetStatusCount($projectId);
        $count = array(
            0 => $gxStatusCount[0]+$matchingStatusCount[0]+$rentStatusCount[0],//未审核数
            1 => $gxStatusCount[1]+$matchingStatusCount[1]+$rentStatusCount[1],//通过数
            2 => $gxStatusCount[2]+$matchingStatusCount[2]+$rentStatusCount[2],//不通过数
        );
        $sumCount = array_sum($count);
        if($sumCount > 0){
            if ($count[0] == $sumCount){
                return 0;//未审核
            }elseif ($count[1] == $sumCount){
                return 1;//通过
            }elseif ($count[2] == $sumCount){
                return 2;//不通过
            }else{
                return 4;//部分通过
            }
        }else{
            return 0;
        }
    }
    /**
     * 项目预算一键审核
     * @param int $id
     * @param number $audit
     * @param array $data
     * @return boolean
     */
    function settleOneKeyAudit($projectId, $audit = 1, $user = array()){
        $this->CI->load->model('carGxModel');
        $this->CI->load->model('carMatchingModel');
        $this->CI->load->model('carRentModel');
        $this->CI->carGxModel->settleAuditByProjectId($projectId, $audit, $user);
        $this->CI->carMatchingModel->settleAuditByProjectId($projectId, $audit, $user);
        $this->CI->carRentModel->settleAuditByProjectId($projectId, $audit, $user);
        $this->edit($projectId, array('settleAuditRole'=>$user['userRole']));
        $this->settleUpdateStatus($projectId);
        //更新金额
        $this->updateMoney($projectId);
        return true;
    }
    /**
     * 更新结算审核状态（结算审核统计，获取结算审核结果）
     * @param number $id 项目ID
     * @return object
     */
    function settleUpdateStatus($projectId){
        $data['settleAuditRs'] = $this->getSettleStatus($projectId);
        $data['settleAuditRs'] == 0 && $data['settleAuditRole'] = 0;
        return $this->edit($projectId, $data);
    }
    /**
     * 获取项目结算审核状态值
     * @param int $id
     */
    function getSettleStatus($projectId){
        $this->CI->load->model('carGxModel');
        $this->CI->load->model('carMatchingModel');
        $this->CI->load->model('carRentModel');
        $gxStatusCount = $this->CI->carGxModel->getSettleStatusCount($projectId);
        $matchingStatusCount = $this->CI->carMatchingModel->getSettleStatusCount($projectId);
        $rentStatusCount = $this->CI->carRentModel->getSettleStatusCount($projectId);
        $count = array(
            0 => $gxStatusCount[0]+$matchingStatusCount[0]+$rentStatusCount[0],//未审核数
            1 => $gxStatusCount[1]+$matchingStatusCount[1]+$rentStatusCount[1],//通过数
            2 => $gxStatusCount[2]+$matchingStatusCount[2]+$rentStatusCount[2],//不通过数
            3 => $gxStatusCount[3]+$matchingStatusCount[3]+$rentStatusCount[3],//返回修改
        );
        $sumCount = array_sum($count);
        if($sumCount > 0){
            if ($count[0] == $sumCount){
                return 0;//未审核
            }elseif ($count[1] == $sumCount){
                return 1;//通过
            }elseif ($count[2] == $sumCount){
                return 2;//不通过
            }elseif ($count[3] == $sumCount){
                return 3;//返回修改
            }else{
                return 4;//部分通过
            }
        }else{
            return 0;
        }
    }
    /**
     * 地方车展金额明细列表
     * @param array $carshowList
     */
    function getMoneyDetailList($carshowList = array()){
        $initData = array(
            'storeNum' => 0,//参展专营店数量
            'gx' => array(
                'budgetApply' => 0,
                'budgetAudit' => 0,
                'settleApply' => 0,
                'settleAudit' => 0,
            ),
            'rent' => array(//场租
                'budgetApply' => 0,
                'budgetAudit' => 0,
                'settleApply' => 0,
                'settleAudit' => 0,
                'budgetRmpSupport' => 0,
                'settleRmpSupport' => 0,
            ),
            'matching' => array(
                'budgetApply' => 0,
                'budgetAudit' => 0,
                'settleApply' => 0,
                'settleAudit' => 0,
                'venueBudgetApply' => 0,//场馆配套预算申请
                'activityBudgetApply' => 0,//活动组织预算申请
                'venueBudgetAudit' => 0,//场馆配套预算审核
                'activityBudgetAudit' => 0,//活动组织预算审核
                'venueSettleApply' => 0,//场馆配套结算申请
                'activitySettleApply' => 0,//活动组织结算申请
                'venueSettleAudit' => 0,//场馆配套结算审核
                'activitySettleAudit' => 0,//活动组织结算审核
            ),
        );
        $returnList = array();
        //初始化
        foreach ($carshowList as $k => $v){
            isset($returnList[$k]) || $returnList[$k] = $initData;
        }
        $projectIds = array_keys($carshowList);
        
        $this->CI->load->model('carGxModel');
        $this->CI->load->model('carRentModel');
        $this->CI->load->model('carMatchingModel');
        //广宣
        $gxList = $this->CI->carGxModel->getList(array('projectId'=>array('in',$projectIds)));
        foreach ($gxList as $v){
            $returnList[$v['projectId']]['gx']['budgetApply'] += $v['budgetApply'];
            $returnList[$v['projectId']]['gx']['budgetAudit'] += $v['budgetAudit'];
            $returnList[$v['projectId']]['gx']['settleApply'] += $v['settleApply'];
            $returnList[$v['projectId']]['gx']['settleAudit'] += $v['settleAudit'];
        }
        //场租
        $rentList = $this->CI->carRentModel->getList(array('projectId'=>array('in',$projectIds)));
        foreach ($rentList as $v){
            $returnList[$v['projectId']]['storeNum'] = $v['storeNum'];
            $returnList[$v['projectId']]['rent']['budgetApply'] += $v['budgetRmpSupport'];
            if (in_array($v['budgetAuditRs'], array(1,4))){
                $returnList[$v['projectId']]['rent']['budgetAudit'] += $v['budgetRmpSupport'];
            }
            if ($v['isSettle'] == 1){
                $returnList[$v['projectId']]['rent']['settleApply'] += $v['settleRmpSupport'];
                if (in_array($v['settleAuditRs'], array(1,4))){
                    $returnList[$v['projectId']]['rent']['settleAudit'] += $v['settleRmpSupport'];
                }
            }
            $returnList[$v['projectId']]['rent']['budgetRmpSupport'] += $v['budgetRmpSupport'];
            $returnList[$v['projectId']]['rent']['settleRmpSupport'] += $v['settleRmpSupport'];
        }
        //配套
        $matchingList = $this->CI->carMatchingModel->getList(array('projectId'=>array('in',$projectIds)));
        foreach ($matchingList as $v){
            $key = $v['type'] == 1 ? 'venue' : 'activity';
            $returnList[$v['projectId']]['matching']['budgetApply'] += $v['budgetApply'];
            $returnList[$v['projectId']]['matching']['budgetAudit'] += $v['budgetAudit'];
            $returnList[$v['projectId']]['matching']['settleApply'] += $v['settleApply'];
            $returnList[$v['projectId']]['matching']['settleAudit'] += $v['settleAudit'];
            
            $returnList[$v['projectId']]['matching'][$key.'BudgetApply'] += $v['budgetApply'];
            $returnList[$v['projectId']]['matching'][$key.'BudgetAudit'] += $v['budgetAudit'];
            $returnList[$v['projectId']]['matching'][$key.'SettleApply'] += $v['settleApply'];
            $returnList[$v['projectId']]['matching'][$key.'SettleAudit'] += $v['settleAudit'];
        }
        return $returnList;
    }
}